package com.youyou.sso.controller;

import com.youyou.sso.bean.UserInfo;
import com.youyou.sso.service.ISsoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * //TODO 添加类/接口功能描述
 *
 * @author 刘朋
 * <br/>date 2021-09-06
 */
@Api(value = "SsoController", tags = "test")
@RestController("")
@RequestMapping("/test")
public class SsoController {

    @Autowired
    private ISsoService iSsoService;

    /**
     * 登录返回token
     * @param userName
     * @param password
     * @return
     */
    @ApiOperation(value = "|登录返回token|")
    @GetMapping("/loginByUserInfo")
    public String login(String userName , String password){
        return iSsoService.login(userName , password);
    }

    /**
     * 查询用户信息
     * @return
     */
    @ApiOperation(value = "|查询用户信息|")
    @GetMapping("/getUserInfo")
    public UserInfo getUserInfo(){
        //获取token
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String authorization = request.getHeader("Authorization");

        return iSsoService.getUserInfo(authorization);
    }

    /**
     * 通过token实现自动登录
     * @param token
     * @return
     */
    @ApiOperation(value = "|通过token实现自动登录|")
    @GetMapping("/loginByToken")
    public boolean login(String token){
        return iSsoService.login(token);
    }
}
